﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace AEM.Controls
{
    /// <summary>
    /// StatusModel.xaml 的交互逻辑
    /// </summary>
    public partial class StatusModel : UserControl
    {
        private string IsUn = "Un";
        public StatusModel()
        {
            InitializeComponent();
        }

        private static void OnStatusTypeChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
        {
            //if (args.NewValue != args.OldValue)
            //{
            (sender as StatusModel).status_image.Style =
                (Style)(sender as StatusModel).TryFindResource("Status"
                + (sender as StatusModel).IsUn
                + ((Data.Enum.LiftStatus)args.NewValue).ToString());
            if (string.IsNullOrEmpty((sender as StatusModel).Text))
                (sender as StatusModel).status_name.Text = (string)(sender as StatusModel).TryFindResource(((Data.Enum.LiftStatus)args.NewValue).ToString());
            //}
        }

        private static void OnIsOnChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
        {
            if (args.NewValue != args.OldValue)
            {
                if ((bool)args.NewValue)
                {
                    //(sender as StatusModel).status_name.Foreground = Brushes.White;
                    (sender as StatusModel).status_name.Visibility = Visibility.Visible;
                    (sender as StatusModel).IsUn = "";
                }
                else
                {
                   // (sender as StatusModel).status_name.Foreground = Brushes.Gray;
                    if ((sender as StatusModel).TextVisibility != Visibility.Visible)
                        (sender as StatusModel).status_name.Visibility = Visibility.Hidden;
                    (sender as StatusModel).IsUn = "Un";
                }
                (sender as StatusModel).status_image.Style =
                    (Style)(sender as StatusModel).TryFindResource("Status"
                    + (sender as StatusModel).IsUn
                    + ((sender as StatusModel).StatusType).ToString());
            }
        }

        private static void OnTextChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
        {
            if (args.NewValue != args.OldValue)
            {
                (sender as StatusModel).status_name.Text = (string)args.NewValue;
            }
        }

        private static void OnTextVisibilityChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
        {
            if (args.NewValue != args.OldValue)
            {
                if ((Visibility)args.NewValue == Visibility.Visible)
                    (sender as StatusModel).status_name.Visibility = (Visibility)args.NewValue;
            }
        }

        public static readonly DependencyProperty StatusTypeProperty =
            DependencyProperty.Register("StatusType", typeof(Data.Enum.LiftStatus), typeof(StatusModel),
            new FrameworkPropertyMetadata(Data.Enum.LiftStatus.Normal, new PropertyChangedCallback(OnStatusTypeChanged)));

        public Data.Enum.LiftStatus StatusType
        {
            get { return (Data.Enum.LiftStatus)GetValue(StatusTypeProperty); }
            set { SetValue(StatusTypeProperty, value); }
        }

        public static readonly DependencyProperty IsOnProperty =
            DependencyProperty.Register("IsOn", typeof(bool), typeof(StatusModel),
            new FrameworkPropertyMetadata(false, new PropertyChangedCallback(OnIsOnChanged)));

        public bool IsOn
        {
            get { return (bool)GetValue(IsOnProperty); }
            set { SetValue(IsOnProperty, value); }
        }

        public static readonly DependencyProperty TextProperty =
            DependencyProperty.Register("Text", typeof(string), typeof(StatusModel),
            new FrameworkPropertyMetadata(string.Empty, new PropertyChangedCallback(OnTextChanged)));

        public string Text
        {
            get { return (string)GetValue(TextProperty); }
            set { SetValue(TextProperty, value); }
        }

        public static readonly DependencyProperty TextVisibilityProperty =
            DependencyProperty.Register("TextVisibility", typeof(Visibility), typeof(StatusModel),
            new FrameworkPropertyMetadata(Visibility.Hidden, new PropertyChangedCallback(OnTextVisibilityChanged)));

        public Visibility TextVisibility
        {
            get { return (Visibility)GetValue(TextVisibilityProperty); }
            set { SetValue(TextVisibilityProperty, value); }
        }
    }
}
